﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Htss.Platform.Data
{
    public class DbAction
    {
        //
        List<DbFields> gFields = new List<DbFields>();
        //
        DbFields Field = null;
        public String TableName { get; set; }
        /// <summary>
        /// 构造函数
        /// </summary>
        public DbAction(){}
        /// <summary>
        /// 构造函数(实例化时自动调用)
        /// </summary>
        /// <param name="tablename">表名</param>
        public DbAction(string tablename)
        {
            this.TableName = tablename;
        }
        /// <summary>
        /// 设置字段
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void SetFields(string key, object value)
        {
            Field = new DbFields();
            //
            Field.Name = key;
            Field.Value = value;
            gFields.Add(Field);
        }
        /// <summary>
        /// 设置字段
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="DbFieldType"></param>
        public void SetFields(string key, object value,DbFieldType DbFieldType)
        {
            Field = new DbFields();
            //
            Field.Name = key;
            Field.Value = value;
            Field.DbFieldType = DbFieldType;
            gFields.Add(Field);
        }
        /// <summary>
        /// 设置字段
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="sqldbType"></param>
        /// <param name="Size"></param>
        public void SetFields(string key, object value, SqlDbType sqldbType,int Size)
        {
            Field = new DbFields();
            //
            Field.Name = key;
            Field.Value = value;
            Field.SqlDbType = sqldbType;
            Field.Size = Size;
            gFields.Add(Field);
        }
        /// <summary>
        /// 设置字段
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="sqldbType"></param>
        /// <param name="Size"></param>
        /// <param name="DbFieldType"></param>
        public void SetFields(string key, object value, SqlDbType sqldbType, int Size,DbFieldType DbFieldType)
        {
            Field = new DbFields();
            //
            Field.Name = key;
            Field.Value = value;
            Field.SqlDbType = sqldbType;
            Field.Size = Size;
            Field.DbFieldType = DbFieldType;
            gFields.Add(Field);
        }
        /// <summary>
        /// 根据自定义SQL进行增删改
        /// </summary>
        /// <param name="sqlstr"></param>
        /// <returns></returns>
        public bool ExecuteSqlNonQuery(string sqlstr)
        {
            DataAccess da=new DataAccess();
            return da.ExecuteSqlNonQuery(sqlstr, GetParameters());
        }
        /// <summary>
        /// 查询(根据自定义SQL语句)
        /// </summary>
        /// <param name="sqlstr">SQL语句</param>
        /// <param name="bo">暂无作用</param>
        /// <returns></returns>
        public DataTable Select(string sqlstr, bool bo)
        {
            DataAccess da = new DataAccess();
            return da.Select(sqlstr, GetParameters()).Tables[0];
        }
        public DataTable SelectNonQuery(string sqlstr)
        {
            DataAccess da = new DataAccess();
            return da.Select(sqlstr, GetParameters()).Tables[0];
        }
        /// <summary>
        /// 根据自定义SQL语句查询 并返回自定义数据集
        /// </summary>
        /// <param name="sqlstr"></param>
        /// <returns></returns>
        public DbTable SelectForReader(string sqlstr)
        {
            DataAccess da = new DataAccess();

            return da.ExecuteSQL(sqlstr, GetParameters());
        }
        /// <summary>
        /// 查询(只允许单条件:非字符串类型字段)
        /// </summary>
        /// <returns></returns>
        public DataTable Select()
        {
            StringBuilder str = new StringBuilder();
            str.Append("select ");
            
            StringBuilder where = new StringBuilder();
            foreach (DbFields f in gFields)
            {
                if (f.DbFieldType == DbFieldType.Query)
                {
                    str.Append(f.Name);
                    str.Append(",");
                }
                if (f.DbFieldType == DbFieldType.Condition || f.DbFieldType == DbFieldType.PrimaryKey)
                {
                    where.Append(" where ");
                    where.Append(f.Name);
                    where.Append("=@");
                    where.Append(f.Name);
                }
            }
            str = str.Remove(str.Length - 1, 1);
            str.Append(" from ");
            str.Append(TableName);
            str.Append(" ");
            str.Append(where);
            DataAccess da = new DataAccess();
            return da.Select(str.ToString(), GetParameters()).Tables[0];
        }
        /// <summary>
        /// 查询(自定义条件查询)
        /// </summary>
        /// <returns></returns>
        public DataTable Select(string where)
        {
            StringBuilder str = new StringBuilder();
            str.Append("select ");
            foreach (DbFields f in gFields)
            {
                if (f.DbFieldType == DbFieldType.Query)
                {
                    str.Append(f.Name);
                    str.Append(",");
                }
            }
            str = str.Remove(str.Length - 1, 1);
            str.Append(" from ");
            str.Append(TableName);
            if (where != "")
            {
                str.Append(" where ");
                str.Append(where);
            }
            DataAccess da = new DataAccess();
            return da.Select(str.ToString(), GetParameters()).Tables[0];
        }
        /// <summary>
        /// 带条件的删除
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public Boolean Delete(string where)
        {
        
            StringBuilder str = new StringBuilder();
            str.Append("delete from ");
            str.Append(TableName);
            str.Append(" where ");
            str.Append(where);
            DataAccess da = new DataAccess();
            return da.ExecuteSqlNonQuery(str.ToString(), GetParameters());
        }
        /// <summary>
        /// 删除(只允许单条件删除:非字符串类型字段)
        /// </summary>
        /// <returns></returns>
        public Boolean Delete()
        {
            StringBuilder str = new StringBuilder();
            str.Append("delete from ");
            str.Append(TableName);
            foreach (DbFields f in gFields)
            {
                if (f.DbFieldType == DbFieldType.Condition || f.DbFieldType == DbFieldType.PrimaryKey)
                {
                    str.Append(" where ");
                    str.Append(f.Name);
                    str.Append("=@");
                    str.Append(f.Name);
                }
            }
            DataAccess da = new DataAccess();
            List<SqlParameter> list = GetParameters();
            object GetID = list.Count;
            return da.ExecuteSqlNonQuery(str.ToString(), GetParameters());
        }
        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="where">自定义条件</param>
        /// <returns></returns>
        public Boolean Update(string where)
        {
            StringBuilder str = new StringBuilder();
            str.Append("update ");
            str.Append(TableName);
            str.Append(" set ");
            foreach (DbFields f in gFields)
            {
                //
                if (f.DbFieldType == DbFieldType.Ordinary)
                {
                    str.Append(f.Name);
                    str.Append("=@");
                    str.Append(f.Name);
                    str.Append(",");
                }
            }
            str = str.Remove(str.Length - 1, 1);
            str.Append(" where ");
            str.Append(where);
            DataAccess da = new DataAccess();
            return da.ExecuteSqlNonQuery(str.ToString(), GetParameters());
        }
        /// <summary>
        /// 修改数据(只允许单条件:非字符串类型字段)
        /// </summary>
        /// <returns></returns>
        public Boolean Update()
        {
            StringBuilder str = new StringBuilder();
            str.Append("update ");
            str.Append(TableName);
            str.Append(" set ");
            StringBuilder where = new StringBuilder();
            where.Append(" where ");
            foreach (DbFields f in gFields)
            {
                //条件
                if (f.DbFieldType == DbFieldType.Condition||f.DbFieldType==DbFieldType.PrimaryKey)
                {
                    where.Append(f.Name);
                    where.Append("=@");
                    where.Append(f.Name);
                }
                //
                if (f.DbFieldType == DbFieldType.Ordinary)
                {
                    str.Append(f.Name);
                    str.Append("=@");
                    str.Append(f.Name);
                    str.Append(",");
                }
            }
            str = str.Remove(str.Length - 1, 1);
            str.Append(where);
            DataAccess da = new DataAccess();
            return da.ExecuteSqlNonQuery(str.ToString(), GetParameters());
        }
        /// <summary>
        /// 插入数据
        /// </summary>
        /// <returns></returns>
        public Boolean Insert()
        {
            StringBuilder str = new StringBuilder();

            str.Append("insert into ");
            str.Append(TableName);
            str.Append("(");
            //
            foreach (DbFields f in gFields)
            {
                if (f.DbFieldType == DbFieldType.Ordinary)
                {
                    str.Append(f.Name);
                    str.Append(",");
                }
            }
            str = str.Remove(str.Length - 1, 1);
            str.Append(") values (");
            //
            foreach (DbFields f in gFields)
            {
                if (f.DbFieldType == DbFieldType.Ordinary)
                {
                    str.Append("@");
                    str.Append(f.Name);
                    str.Append(",");
                }
            }
            str = str.Remove(str.Length - 1, 1);
            str.Append(")");

            DataAccess da = new DataAccess();
            return da.ExecuteSqlNonQuery(str.ToString(), GetParameters());
        }
        /// <summary>
        /// 获取到参数
        /// </summary>
        /// <returns></returns>
        private List<SqlParameter> GetParameters()
        {
            List<SqlParameter> list=new List<SqlParameter>();
            SqlParameter parameter;
            //循环哈希表 把哈希表中的数据取出来 存放到list 中
            foreach(DbFields f in gFields)
            {
                if (f.DbFieldType != DbFieldType.Query)
                {
                    parameter = new SqlParameter();
                    parameter.ParameterName = "@" + f.Name;
                    parameter.Value = f.Value;
                    if (f.SqlDbType != 0)
                    {
                        parameter.SqlDbType = f.SqlDbType;
                    }
                    if (f.Size != 0)
                    {
                        parameter.Size = f.Size;
                    }
                    list.Add(parameter);
                }
            }
            return list;
        }
    }
}
